package cn.hfatec.shms.web.xh.xhHfjl.action;

import cn.hfatec.shms.web.yk.ykHsb.model.YkHsb;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Resource;


import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.web.xh.xhHfjl.model.XhHfjlParams;
import cn.hfatec.shms.web.xh.xhHfjl.model.XhHfjl;
import cn.hfatec.shms.web.xh.xhHfjl.service.XhHfjlService;
import cn.hfatec.shms.base.ShmsBaseAction;
import org.springframework.ui.Model;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;



/**
 *
 * @author hfatec codegen
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Controller
@RequestMapping("/xh/xhHfjl")
public class XhHfjlAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XhHfjlService")
    private XhHfjlService xhHfjlService;


    /**
     *
     * 查询列表
     *
     */
    @RequestMapping("/list")
    public String list(XhHfjlParams xhHfjlParams, Page page, Model model) {

        Table table = xhHfjlService.queryXhHfjlTable(xhHfjlParams, page);

        model.addAttribute("xhHfjlsTable", table);
        model.addAttribute("xhHfjlParams", xhHfjlParams);
        return "/xh/xhHfjl/list";
    }

    /**
     *
     * 异步查询列表
     *
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XhHfjlParams xhHfjlParams, Page page, Model model) {
        try {
            list(xhHfjlParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xh/xhHfjl/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到新增页
     *
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            XhHfjl xhHfjl = new XhHfjl();
            model.addAttribute("xhHfjl", xhHfjl);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xh/xhHfjl/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 跳转到编辑页
     *
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            XhHfjl xhHfjl = xhHfjlService.getXhHfjl(id);
            model.addAttribute("xhHfjl", xhHfjl);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xh/xhHfjl/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     *
     * 根据IDs删除数据
     *
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXhHfjl(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if(ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            xhHfjlService.deleteXhHfjlBatch(idList);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     *
     * 保存数据
     *
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXhHfjl(@Valid XhHfjl xhHfjl, BindingResult result) {

        try {
            if(result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            xhHfjlService.saveXhHfjl(xhHfjl);
        } catch(Exception e) {
            e.printStackTrace();
            logger.error("数据XhHfjl保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goCheck")
    @ResponseBody
    public String goCheck(String executeStart, String executeEnd) {
        try {


            xhHfjlService.deleteXhHfjls(executeStart,executeEnd);

            List<XhHfjl> listxh=xhHfjlService.getXhHfjlList(executeStart,executeEnd);

            for (XhHfjl xhHfjl : listxh) {
                xhHfjlService.saveXhHfjl(xhHfjl);
            }

            if (listxh != null) {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", listxh).toString();
            } else {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", new XhHfjl()).toString();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

}
